// pages/agree/index.js
// pages/Detail/detail.js
const WxParse = require('../Common/wxParse/wxParse.js');
const Controller = require('../controllers/controllers.js').controller;

Page({

    /**
     * 页面的初始数据
     */
    data: {
      content:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    
    let title= options.key=='key-user-agreement'?'用户协议':"隐私条款"
     wx.setNavigationBarTitle({
       title
     })
     console.log('option',options)
     if(options.key){
      this.getInfo(options.key)
     }
     
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    
    },
  // 获取协议内容
    getInfo(key){

      Controller.appinfo({key}).then(res=>{
       if(res.code==0){
         let content=res.data.baseData.content
         this.setData({
          content
        })

         WxParse.wxParse('content', 'html', this.formatRichText(content) , this, 5);
  
       
       }
      })

    },

     //格式化富文本
     formatRichText(html) {
      if (html) {
          let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
              match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
              match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
              match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
              return match;
          });
          newContent = newContent.replace(/<table[^>]*>/gi, function(match, capture) {
              match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
              match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
              match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
              return match;
          });
          newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
              match = match.replace(/width:[^;]+;/gi, 'max-width:100%;vertical-align:middle;')
              return match;
          });
          // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
          newContent = newContent.replace(/\<img/gi,
              '<img style="max-width:100%;vertical-align:middle;"');
          return newContent;
      }
  },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})